cmake_minimum_required(VERSION 3.15)
project(cloud_storage_service_v4_v5 CXX)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

# ----------------- 1. 查找所有主要的外部依赖包 -----------------
find_package(Protobuf REQUIRED)
find_package(gRPC REQUIRED)
find_package(Threads REQUIRED)
find_package(CURL REQUIRED)
find_package(ppconsul REQUIRED)

## ----------------- 2. 手动定义 .proto 文件的代码生成规则 -----------------
#set(PROTO_DIR ${CMAKE_CURRENT_SOURCE_DIR}/protos)
#set(GENERATED_DIR ${CMAKE_CURRENT_SOURCE_DIR}/src/generated)
#file(GLOB PROTO_FILES "${PROTO_DIR}/*.proto")

#set(PROTO_GENERATED_SOURCES "")
#foreach(PROTO_FILE ${PROTO_FILES})
    #get_filename_component(PROTO_FILENAME ${PROTO_FILE} NAME_WE)
    #set(PB_CC_FILE "${GENERATED_DIR}/${PROTO_FILENAME}.pb.cc")
    #set(PB_H_FILE "${GENERATED_DIR}/${PROTO_FILENAME}.pb.h")
    #set(GRPC_CC_FILE "${GENERATED_DIR}/${PROTO_FILENAME}.grpc.pb.cc")
    #set(GRPC_H_FILE "${GENERATED_DIR}/${PROTO_FILENAME}.grpc.pb.h")
    #list(APPEND PROTO_GENERATED_SOURCES ${PB_CC_FILE} ${GRPC_CC_FILE})
    #add_custom_command(
        #OUTPUT ${PB_CC_FILE} ${PB_H_FILE} ${GRPC_CC_FILE} ${GRPC_H_FILE}
        #COMMAND ${Protobuf_PROTOC_EXECUTABLE}
        #ARGS --grpc_out=${GENERATED_DIR} --cpp_out=${GENERATED_DIR} -I ${PROTO_DIR}
             #--plugin=protoc-gen-grpc=${gRPC_CPP_PLUGIN_EXECUTABLE}
             #${PROTO_FILE}
        #DEPENDS ${PROTO_FILE}
        #COMMENT "Generating C++ sources from ${PROTO_FILE}"
    #)
#endforeach()

#add_library(cloud_storage_proto ${PROTO_GENERATED_SOURCES})
#target_include_directories(cloud_storage_proto PUBLIC ${GENERATED_DIR})

set(GENERATED_DIR ${CMAKE_CURRENT_SOURCE_DIR}/src/generated)
add_library(cloud_storage_proto
    ${GENERATED_DIR}/protos/user_service.pb.cc
    ${GENERATED_DIR}/protos/user_service.grpc.pb.cc
    ${GENERATED_DIR}/protos/file_service.pb.cc
    ${GENERATED_DIR}/protos/file_service.grpc.pb.cc
)
# 为这个库添加头文件路径
target_include_directories(cloud_storage_proto PUBLIC ${GENERATED_DIR})


# ----------------- 3. 创建共享的核心业务逻辑库 (core_logic) -----------------
add_library(core_logic STATIC
    src/common/config.cpp 
    src/common/response.cpp
    src/database/db_connection.cpp 
    src/database/file_dao.cpp 
    src/database/user_dao.cpp
    src/utils/crypto.cpp 
    src/utils/file_utils.cpp 
    src/utils/oss_client_manager.cpp
    src/utils/rabbitmq_client.cpp
    src/utils/token.cpp
    src/utils/service_discovery.cpp
    src/handlers/user_handler.cpp 
    src/handlers/file_handler.cpp
)
target_include_directories(core_logic PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include ${GENERATED_DIR})
target_link_libraries(core_logic PRIVATE
    Threads::Threads 
    alibabacloud-oss-cpp-sdk 
    SimpleAmqpClient
    workflow
    mysqlclient 
    ssl 
    crypto
    CURL::libcurl
    ppconsul
)

# ----------------- 4. 创建所有微服务的可执行文件目标 -----------------
# 注意看这里的链接变化

add_executable(gateway_service src/gateway_service/main.cpp)
target_link_libraries(gateway_service PRIVATE core_logic cloud_storage_proto gRPC::grpc++ ${Protobuf_LIBRARIES})

add_executable(user_service src/user_service/main.cpp)
target_link_libraries(user_service PRIVATE core_logic cloud_storage_proto gRPC::grpc++ ${Protobuf_LIBRARIES})

add_executable(file_service src/file_service/main.cpp)
target_link_libraries(file_service PRIVATE core_logic cloud_storage_proto gRPC::grpc++ ${Protobuf_LIBRARIES})

add_executable(transfer_service src/transfer_service/main.cpp)
target_link_libraries(transfer_service PRIVATE core_logic)
